package br.edu.fcsl.web.servelets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.edu.fcsl.entidade.Produto;
import br.edu.fcsl.persistencia.ProdutoDAO;
import br.edu.fcsl.persistencia.UsuarioDAO;

/**
 * Servlet implementation class ServletCrud
 */
@WebServlet("/ServletCrud")
public class ServletCrud extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletCrud() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		 int id = Integer.parseInt(request.getParameter("id"));
		 
		 HttpSession session = request.getSession();
		 
		 String apagar =  String.valueOf(request.getParameter("Apagar"));
		String editar = String.valueOf(request.getParameter("Editar"));
	
		 
		
		 if(apagar.equals("Apagar") ){
			 
		
			 ProdutoDAO dao= new ProdutoDAO();
			Produto prd = new Produto();
			 
			 prd.setId(id);
			 
			
			 dao.excluir(prd);
			 response.sendRedirect("admin/listaProdutos.jsp");
			 
			 
		 } 
		 if(editar.equals("Editar")){
			 
			 System.out.println("ok");
			 session.setAttribute("EditarId",request.getParameter("id") );
			 session.setAttribute("EditarDescri", request.getParameter("desc"));
			 session.setAttribute("EditarUnidade",request.getParameter("uni"));
			 session.setAttribute("EditarValor", request.getParameter("valor"));
			 
			 
			 
			 
			 response.sendRedirect("admin/editarProdutos.jsp");
			 
			 
		 }
			 
			 
	 
		 
		
		
		
		
		
		
		
	}

}
